/**
 * Created by alice on 2017/5/04 0001
 */
;-function (window) {
    "use strict";
    //TODO 预设变量写这里，全局变量写在base.js 里面 使用 r. 调用
    var ready = {
        layer: null,
        city: null,
        secretary: null,
        start: null,
        end: null,
        current_court_id: null,
        case_type: null,
        _user: null,
        courtrooms_id: null,
        page_size: 12,
        page_index: 1
    };
    //TODO 初始化的方法写这里面
    var init = function () {
        init_ready();//加载预设变量

        $('#startTime').datetimepicker({
            bootcssVer: 3,
            minView: "month", //选择日期后，不会再跳转去选择时分秒
            weekStart: "1",
            format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
            language: 'zh-CN', //汉化
            autoclose: true //选择日期后自动关闭
        }).on('changeDate', function (ev) {
            // console.log(ev.date.valueOf());
        });
        var end_time = $('#endTime');
        $('#endTime').datetimepicker({
            bootcssVer: 3,
            minView: "month", //选择日期后，不会再跳转去选择时分秒
            weekStart: "1",
            format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
            language: 'zh-CN', //汉化
            autoclose: true //选择日期后自动关闭
        }).on('changeDate', function (ev) {
            // console.log(ev.date.valueOf());
        });

        init_event();
        function init_event() {//TODO 初始化页面事件

            if (ready.start) {
                $("#startTime").find("input").val(ready.start);
            }
            if (ready.end) {
                end_time.find("input").val(ready.end);
            } else {
                end_time.find("input").val(utils.get_url_param(0));
            }

            //点击查询按钮
            $("#conditionBtn").click(function () {
                // $("#loading").show();
                Class.count_case_review();
            });
            Class.select_room();
            Class.select_secretary();
            Class.select_case_type();
            Class.count_case_review();
            $("#loading").hide();
        }

        function init_ready() {//TODO 初始化预设值
            layui.use('layer', function () {
                ready.layer = layui.layer;
            });
            ready.city = r.user_court_uid;
            var judge_uid = utils.get_url_param("judge");
            judge_uid ? ready.judge.push(judge_uid) : '';
            ready.secretary = utils.get_url_param("secretary");
            ready.start = utils.get_url_param("start");
            ready.end = utils.get_url_param("end");
            ready.current_court_uid = utils.get_url_param("currentCourtUid");
            ready.case_type = utils.get_url_param("caseType");

        }
    };
    //TODO 内部调用的方法写这里
    var Class = {
        select_room: function () {
            if (courtroomList) {
                var court_room = $("#allCourtRooms").empty();
                court_room.append("<option value=''>" + "所有法庭" + "</option>");
                $.each(courtroomList, function (i, valueOfElement) {
                    if (valueOfElement.courtUid == r.user_court_uid && valueOfElement.active == 1) {
                        court_room.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                    }
                });
            }
        },
        select_case_type: function () {
            if (caseTypeList) {
                var case_type = $("#caseTypeName").empty();
                // if (null != ready.case_type && '' != ready.case_type && 'null' != ready.case_type) {
                //     $.each(result.data, function (i, valueOfElement) {
                //         if (ready.case_type == valueOfElement.uid) {
                //             case_type.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                //         }
                //     });
                // } else {
                case_type.append("<option value=''>" + "所有类型" + "</option>");
                $.each(caseTypeList, function (i, valueOfElement) {
                    case_type.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                });
                // }
            }
        },
        select_secretary: function () {
            var select_secretary = $("#allSecretary").empty();
            if (ready.secretary) {
                $.get(r.base_path + "sys/selectUserByPrimaryKey", {id: ready.secretary}, function (result) {
                    if (result) {
                        if (result && result.status == 0) {
                            select_secretary.append("<option value='" + result.data[0].uid + "'>" + result.data[0].username + "</option>");
                            select_secretary.find("input").val(result.data[0].uid);
                            $(".active").find("span").append("&nbsp;" + result.data[0].username + "审理过的案件");//今日开庭和排期
                        } else {
                            layui.use('layer', function () {
                                layui.layer.msg("未能找到指定书记员");
                            });
                        }
                    }
                }, 'json');

            } else {
                if (secretaryList) {
                    select_secretary.append("<option value=''>" + "所有书记员" + "</option>");
                    $.each(secretaryList, function (i, valueOfElement) {
                        select_secretary.append("<option value='" + valueOfElement.uid + "'>" + valueOfElement.name + "</option>");
                    });
                }
            }
        },
        count_case_review: function (pageIndex) {
            if (!pageIndex) {
                pageIndex = 1;
            }
            ready.page_index = pageIndex;
            var archives = $("#caseno").val();
            var caseName = $("#caseName").val();
            var caseTypeName = $("#caseTypeName").val();
            var courtRoom = $("#allCourtRooms").val();
            var secretary = ready.secretary || $("#allSecretary").val();
            var startTime = $("#startTime").find("input").val();
            var endTime = $("#endTime").find("input").val();
            var row_no_padding = $(".row-no-padding").empty();
            // console.log(caseName, caseTypeName, courtRoom, judge, secretary, startTime, endTime);
            $.ajax({
                url: r.base_path + "judge/selectMoreCasePlanOrReview",
                type: "get",
                dataType: "json",
                data: {
                    archives: archives,
                    caseName: caseName,
                    type: caseTypeName,
                    courtRoom: courtRoom,
                    judgeList: r.user_uid,
                    secretaryId: secretary,
                    startDate: startTime,
                    endDate: endTime,
                    courtUid: r.user_court_uid,
                    status: 0,
                    review: 1,
                    page: pageIndex,
                    pageSize: ready.page_size
                },
                success: function (result) {
                    // console.log(result.data);
                    var s = result.data.count;
                    if (s == 0) {
                        Class.page(1, 1, null);
                    } else {
                        Class.page(pageIndex, Math.ceil(s / 12), Class.count_case_review);
                    }

                    if (result.data.count <= 0) {
                        row_no_padding.append('<span>暂无开庭信息</span>');
                    } else {
                        var picNum = 6;
                        $.each(result.data.data, function (i, valueOfElement) {

                            var caseName = valueOfElement.name;
                            var startDate = valueOfElement.startDate;
                            var status = valueOfElement.status;
                            var courtroomName = valueOfElement.courtroomName;
                            var statusName = "";
                            if (0 == status) {
                                statusName = "排期";
                            } else if (1 == status || 5 == status) {
                                statusName = "开庭";
                            } else if (2 == status) {
                                statusName = "休庭";
                            } else if (3 == status) {
                                statusName = "闭庭";
                            } else {
                                statusName = "归档";
                            }
                            var success = false;
                            if (favorites&&$.inArray(valueOfElement.planUid,favorites)>-1){
                                success = true;
                            }
                            var randomNum = parseInt(picNum * Math.random() + 1);//随机出1到picNum的整数
                            var href = r.base_path + "judge/play?planUid=" + valueOfElement.planUid;
                            var html = '<div class="col-md-2 col-xs-6">' +
                                '<div class="more-reviewing-item">' +
                                '<a href="' + href + '">' +
                                '<div class="more-reviewing-item-img-panel">' +
                                '<p class="more-reviewing-item-img-panel-top">' + (courtroomName ? courtroomName : '未知') + '</p>' +
                                '<img src="' + r.base_path + "assets/img/more_court_test" + randomNum + ".jpg" + '" alt="法院">' +
                                '<p class="more-reviewing-item-img-panel-bottom" >' + statusName + '</p>' +
                                '</div>' +
                                '<p id="startDate">' + startDate + '</p>' +
                                '<p id="caseNoDis">' + (valueOfElement.archives ? valueOfElement.archives : '未知') + '</p>' +
                                '<p class="more-reviewing-item-depiction">' +
                                '<i class="fa fa-thumb-tack ' + (success ? 'status-top' : '') + '" title="收藏" id="' + valueOfElement.planUid + '..' + r.user_court_uid + '"></i>' +
                                '<span>' + (caseName ? caseName : "&nbsp;") + '</span>' +
                                '</p>' +
                                '</a>' +
                                '</div>' +
                                '</div>';
                            row_no_padding.append(html);
                        });
                        $(".fa-thumb-tack").click(function (e) {
                            e.preventDefault();
                            var cid = this.id.split('..');
                            var uid = cid[0];
                            var class_name = '' + this.className;
                            layui.use(['layer', 'form'], function () {
                                var layer = layui.layer;
                                if (class_name.indexOf("status-top") < 0) {
                                    layer.confirm('确定收藏吗？', {
                                        btn: ['是的', '取消'] //按钮
                                    }, function () {
                                        $.post(r.base_path + "favorite/like", {
                                            planUid: uid
                                        }, function (result) {
                                            favorites.push(uid);
                                            layer.msg(result.message);
                                            Class.count_case_review(ready.page_index);
                                        }, 'json')
                                    }, function () {

                                    });
                                } else {
                                    layer.confirm('确定取消收藏吗？', {
                                        btn: ['是的', '取消'] //按钮
                                    }, function () {
                                        $.post(r.base_path + "favorite/unlike", {
                                            planUid: uid
                                        }, function (result) {
                                            var index = favorites.indexOf(uid);
                                            if (index > -1) {
                                                favorites.splice(index, 1);
                                            }
                                            layer.msg(result.message);
                                            Class.count_case_review(ready.page_index);
                                        }, 'json')
                                    }, function () {

                                    });
                                }
                            });
                        });
                    }
                    $("#loading").hide();
                    row_no_padding.show();
                }
            }).fail(function () {
                $("#loading").hide();
                layui.use('layer', function () {
                    layui.layer.msg("载入数据发生了问题");
                });
            });
        },
        page: function (now, totalPages, callback) {
            base.pagination('#_page', {
                currentPage: now,
                totalPages: totalPages,
                onPageClicked: function (page) {
                    callback(page);
                }
            });
        }
    };
    init();
    //TODO 对外公开的方法写在这里
    window.p = {
        get_sum_case: Class.count_case_review
    };
}(window);
